package crawer.datatransaction.extend;

import java.util.Hashtable;
import java.util.Vector;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;

import org.jdom.Element;

import crawer.datatransaction.basic.Pattern;
import crawer.datatransaction.basic.PatternFactory;
import crawer.datatransaction.util.*;
import crawer.util.LogCode;
import crawer.util.RegexUtil;

public class Group extends Pattern {
	public void finalize()
	{
		instanceCount--;
	}
	
	public Group(PatternFactory p_Factory) {
		super(p_Factory);
		super.userObject = new NodeData(Data.Group, Data.Group,
				NodeData.WithoutRetureValue);
		//
		DefaultMutableTreeNode _node = new DefaultMutableTreeNode();
		_node
				.setUserObject(new NodeData(Data.Split, Data.Split,
						NodeData.Leaf));
		super.AddAttribution(_node);

		_node = new DefaultMutableTreeNode();
		_node.setUserObject(new NodeData(Data.Case, Data.False, NodeData.Leaf));
		super.AddAttribution(_node);

		_node = new DefaultMutableTreeNode();
		_node.setUserObject(new NodeData(Data.Regular, Data.False,
				NodeData.Leaf));
		super.AddAttribution(_node);

		_node = new DefaultMutableTreeNode();
		_node
				.setUserObject(new NodeData(Data.Ignore, Data.None,
						NodeData.Leaf));
		super.AddAttribution(_node);
	}

	public boolean LoadSetting(Element elt) {

		Element sub_elt = null;
		DefaultMutableTreeNode sub_node = null;
		try {
			sub_elt = super.getSingleElement(elt, Data.Split);
			String value = super.getElementValue(sub_elt);
			sub_node = super.getSingleNodebyName(Data.Split);
			super.getNodeData(sub_node).setText(value);

			sub_elt = super.getSingleElement(elt, Data.Case);
			if (sub_elt != null) {
				value = super.getElementValue(sub_elt);
				sub_node = super.getSingleNodebyName(Data.Case);
				super.getNodeData(sub_node).setText(value);
			}

			sub_elt = super.getSingleElement(elt, Data.Regular);
			if (sub_elt != null) {
				value = super.getElementValue(sub_elt);
				sub_node = super.getSingleNodebyName(Data.Regular);
				super.getNodeData(sub_node).setText(value);
			}

			sub_elt = super.getSingleElement(elt, Data.Ignore);
			if (sub_elt != null) {
				value = super.getElementValue(sub_elt);
				sub_node = super.getSingleNodebyName(Data.Ignore);
				super.getNodeData(sub_node).setText(value);
			}
		} catch (Exception e) {
			log.info(e);
			return false;
		}
		return super.LoadSetting(elt);
	}

	public Object Operate(String p_DataStream, String p_strUrl,
			Hashtable p_PreData) {		
		try{
			String str_split = super.getNodeData(
					this.getSingleNodebyName(Data.Split)).getText();
			String str_regular = super.getNodeData(
					this.getSingleNodebyName(Data.Regular)).getText();
			String str_case = super
					.getNodeData(this.getSingleNodebyName(Data.Case)).getText();
			//both, none, head,tail
			String str_ignore = super.getNodeData(
					this.getSingleNodebyName(Data.Ignore)).getText();
			boolean is_regular = false;
			boolean case_sense = false;
	
			if (str_regular.equalsIgnoreCase(Data.True)) {
				is_regular = true;
			}
			if (str_case.equalsIgnoreCase(Data.True))
				case_sense = true;
			List<String> result = RegexUtil.split(p_DataStream, str_split,
					is_regular, case_sense);
			//Ignore
			Vector<String> vec = new Vector<String>();
			//fill into vec<String>
			int list_size = result.size();
			for (int i = 0; i < list_size; i++) {
				if (i == 0) {
					if (str_ignore.equalsIgnoreCase(Data.None)
							|| str_ignore.equalsIgnoreCase(Data.Tail))
						vec.add(result.get(i));
				} else if (i == list_size - 1) {
					if (str_ignore.equalsIgnoreCase(Data.None)
							|| str_ignore.equalsIgnoreCase(Data.Head))
						vec.add(result.get(i));
				} else {
					vec.add(result.get(i));
				}
			}
			for (String item : vec) {
				super.Operate(item, p_strUrl, p_PreData);
			}
			//vec.clear();
		}catch(Exception e){
			log.error("[["+LogCode.E_GROUP_FAIL+"]], URL:["+ p_strUrl + "]");
		}
		return null;
	}
}
